// components/tupian-ctrl/tupian-ctrl.js
let userInfo = {}
const db = wx.cloud.database()
let ip = ''
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    tupianid: String,
    tupian: Object,
    dianzan:Boolean,
  },
  externalClasses: ['iconfont', 'icon-pinglun', 'icon-fenxiang'],

  /**
   * 组件的初始数据
   */
  data: {
    // 登录组件是否显示
    loginShow: false,
    // 底部弹出层是否显示
    modalShow: false,
  },

  /**
   * 组件的方法列表
   */
  methods: {

    onComment() {
      // 判断用户是否授权
      wx.getSetting({
        success: (res) => {
          if (res.authSetting['scope.userInfo']) {
            wx.getUserInfo({
              success: (res) => {
                userInfo = res.userInfo
                // 显示评论弹出层
                this.setData({
                  modalShow: true,
                })
              }
            })
          } else {
            this.setData({
              loginShow: true,
            })
          }
        }
      })
    },

    onLoginsuccess(event) {
      userInfo = event.detail
      // 授权框消失，评论框显示
      this.setData({
        loginShow: false,
      }, () => {
        this.setData({
          modalShow: true,
        })
      })
    },

    onLoginfail() {
      wx.showModal({
        title: '授权用户才能进行评价',
        content: '',
      })
    },
    onSend(event) {
      console.log(this.properties.tupian.opid)
      console.log(event)
      // 插入数据库
      let content = event.detail.value.content
      if (content.trim() == '') {
        wx.showModal({
          title: '评论内容不能为空',
          content: '',
        })
        return
      }
      wx.showLoading({
        title: '评论中',
        mask: true,
      })
      db.collection('meitu-pl').add({
        data: {
          content,
          createTime: db.serverDate(),
          tupianid: this.properties.tupianid,
          nickName: userInfo.nickName,
          avatarUrl: userInfo.avatarUrl,
          ip: 200,
          biaoqian_ming: this.properties.tupian.biaoqian_ming,
          opid: this.properties.tupian.opid,
        }
      }).then((res) => {
        wx.hideLoading()
        wx.showToast({
          title: '评论成功',
        })
        this.setData({
          modalShow: false,
          content: '',
        })
        // 父元素刷新评论页面
        this.triggerEvent('refreshCommentList')
      })
    },

    /**
    * 点赞功能
    */
   clickMe: async function () {
      wx.showLoading({
        title: '点赞中...',
      })
      console.log(this.properties.dianzan)
      var dianzan1 = this.properties.dianzan
        //云函数方式操作

      if (this.properties.dianzan==true) {
        wx.cloud.callFunction({
          name: 'dianzan',
          data: {
            dianzan: dianzan1,
            id: this.properties.tupian._id,
            $url: 'delzan'
          }
        }).then((res) => {
          // console.log(res.result)
          this.setData({
            dianzanUrl:"/images/dianzan_2.png",
            dianzan:dianzan1
          })
          wx.showToast({
            title: '点赞取消',
            icon: 'success',
            duration: 1500
          })
        })
      } else {
        wx.cloud.callFunction({
          name: 'dianzan',
          data: {
            dianzan: !dianzan1,
            id: this.properties.tupian._id,
            $url: 'addzan'
          }
        }).then((res) => {
          // console.log(res.result)
          this.setData({
            dianzanUrl:"/images/dianzan_1.png",
            dianzan:!dianzan1
          })
          wx.showToast({
            title: '点赞成功',
            icon: 'success',
            duration: 1500
          })
        }).catch((res) => {
          wx.hideLoading()
          wx.showToast({
            title: '点赞失败',
          })
        })
      }
    },
   
  },

    // //点赞切换
    // clickMe() {
    //   var dianzan = this.properties.tupian.dianzan;
    //   this.setData({
    //     dianzanUrl: dianzan ? "/images/dianzan_1.png" : "/images/dianzan_2.png"
    //   })
    //   dianzan = !dianzan
    //   //云函数方式操作
    //   wx.cloud.callFunction({
    //     //调用的云函数名
    //     name: "dianzan",
    //     data: {
    //       action: "dianzan",
    //       //要上传的数据，在云函数的event里
    //       _id: this.properties.tupianid,
    //       dianzan: this.properties.tupian.dianzan
    //     }
    //   })
    //     .then(res => {
    //       console.log("改变点赞状态成功", res);
    //       this.triggerEvent('refreshCommentList')
    //     })
    //     .catch(err => {
    //       console.log("改变点赞状态失败", err);
    //       this.triggerEvent('refreshCommentList')
    //     })
    // },



  lifetimes: {
    attached: function() {
       console.log(this.properties.dianzan)
      if(this.properties.dianzan==true){
        this.setData({
          dianzanUrl: "/images/dianzan_2.png" 
        })
      }else{
        this.setData({
          dianzanUrl: "/images/dianzan_2.png" 
        })
      }
    },
  },

})